package com.lh.common.utils;


import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

/**
 * @ClassName: MessageUtils
 * @Description: 消息帮助类
 * @author gze
 * @date 2020年6月5日 下午2:46:23
 */
@Component
public class MessageUtils
{
    @Autowired
    private MessageSource messageSource;

    public MessageUtils()
    {
    }

    /**
     * 获取单个国际化翻译值
     */
    public String getMessage(String msgKey) {
        try {
            return messageSource.getMessage(msgKey, null, LocaleContextHolder.getLocale());
        } catch (Exception e) {
            return msgKey;
        }
    }
    /**
     * 获取单个国际化翻译值,
     *
     */
    /**获取单个国际化翻译值,
     　* params:替换的参数
     */
    public String getMessage(String msgKey, Object... params) {
        try {
            String message = messageSource.getMessage(msgKey, null, LocaleContextHolder.getLocale());
            message = String.format(message, params);
            return message;
        } catch (Exception e) {
            return msgKey;
        }
    }


}
